/**
 * Copyright (C) 2013-2014 the original author or authors.
 */
package com.foreveross.bsl.system.application.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.inject.Named;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;

import com.foreveross.bsl.common.utils.bean.BeanHelper;
import com.foreveross.bsl.common.utils.context.ContextThead;
import com.foreveross.bsl.common.utils.enums.AccountStatus;
import com.foreveross.bsl.common.utils.enums.OperatEnum;
import com.foreveross.bsl.common.utils.exception.ApplicationException;
import com.foreveross.bsl.mam.model.rbac.domain.Admin;
import com.foreveross.bsl.system.application.AccountInfoApplication;
import com.foreveross.bsl.system.application.vo.AccountInfoDetailVO;
import com.foreveross.bsl.system.application.vo.AccountInfoVO;
import com.foreveross.bsl.system.model.domain.AccountInfo;

/**
 * @description
 * 
 * @author rening
 * @version v1.0
 * 
 * @2013-6-7
 * 
 */
@Named("accountInfoApplication")
public class AccountInfoApplicationImpl implements AccountInfoApplication {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.foreveross.bsl.system.application.DatabaseInfoApplication#saveEntity
	 * (com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public AccountInfoVO saveEntity(AccountInfoDetailVO entity) {
		AccountInfo accountInfo = new AccountInfo();
		try {
			PropertyUtils.copyProperties(accountInfo, entity);
		} catch (Exception e) {
			throw new ApplicationException(e.getMessage());
		}
		accountInfo = (AccountInfo) accountInfo.save();

		Admin admin = new Admin();
		admin.setUsername(entity.getUsername());
		admin.setName(entity.getName());
		Md5PasswordEncoder md = new Md5PasswordEncoder();
		String encodePassword = md.encodePassword(entity.getPassword(),
				entity.getUsername());
		admin.setPassword(encodePassword);
		admin.setAddress(accountInfo.getAddress());
		admin.setTelephone(accountInfo.getTelephone());
		admin.setStatus(AccountStatus.Normal.getValue());
		ContextThead.setThreadVar(entity.getAccUsername());
		admin.save();

		AccountInfoVO vo = new AccountInfoVO();
		try {
			PropertyUtils.copyProperties(vo, accountInfo);
		} catch (Exception e) {
			throw new ApplicationException(e.getMessage());
		}
		return vo;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.foreveross.bsl.system.application.DatabaseInfoApplication#updateEntity
	 * (com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public AccountInfoVO updateEntity(String id, AccountInfoVO entity) {
		AccountInfo account = new AccountInfo();
		account = (AccountInfo) account.findOne(id);
		BeanHelper.copyProperties(account, entity);
		account.save();
		BeanHelper.copyProperties(entity, account);
		return entity;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.foreveross.bsl.system.application.DatabaseInfoApplication#deleteEntity
	 * (com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public void deleteEntity(String id) {
		AccountInfo account = new AccountInfo();
		account = (AccountInfo) account.findOne(id);
		account.delete();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.foreveross.bsl.system.application.DatabaseInfoApplication#getEntity
	 * (java.lang.String)
	 */
	@Override
	public AccountInfoVO getEntity(String id) {
		AccountInfo account = new AccountInfo();
		account = (AccountInfo) account.findOne(id);
		AccountInfoVO vo = new AccountInfoVO();
		BeanHelper.copyProperties(vo, account);
		return vo;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.foreveross.bsl.system.application.DatabaseInfoApplication#getEntity
	 * (java.lang.String)
	 */
	@Override
	public AccountInfoVO getEntityByAccountId(String accountId) {
		AccountInfo account = new AccountInfo();
		account = (AccountInfo) account.findListByProperty("accountId",
				OperatEnum.EQ, accountId).get(0);
		AccountInfoVO vo = new AccountInfoVO();
		BeanHelper.copyProperties(vo, account);
		return vo;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.foreveross.bsl.system.application.DatabaseInfoApplication#findAll()
	 */
	@Override
	public List<AccountInfoVO> findAll() {
		AccountInfo account = new AccountInfo();
		Iterable iterable = account.findAll();
		if (iterable != null) {
			List<AccountInfoVO> result = new ArrayList<AccountInfoVO>();
			AccountInfoVO vo;
			for (Iterator iterator = iterable.iterator(); iterator.hasNext();) {
				AccountInfo obj = (AccountInfo) iterator.next();

				vo = new AccountInfoVO();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			return result;
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.foreveross.bsl.system.application.DatabaseInfoApplication#findByPageable
	 * (org.springframework.data.domain.Pageable)
	 */
	@Override
	public Page<AccountInfoVO> findByPageable(Pageable pageable) {
		AccountInfo account = new AccountInfo();
		Page page = account.findAll(pageable);
		if (page != null && page.getContent() != null) {
			List<AccountInfoVO> result = new ArrayList<AccountInfoVO>();
			AccountInfoVO vo;
			for (Iterator iterator = page.getContent().iterator(); iterator
					.hasNext();) {
				AccountInfo obj = (AccountInfo) iterator.next();

				vo = new AccountInfoVO();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			Page<AccountInfoVO> pageVO = new PageImpl<AccountInfoVO>(result,
					pageable, page.getTotalElements());
			return pageVO;
		}
		return null;
	}

}
